package com.example.chatfree.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.SignUpCallback;
import com.example.chatfree.R;
import com.example.chatfree.leancloud.AVService;

/**
 * 用户注册页面
 * Created by 周国谱 on 2015/10/8.
 */
public class RegisterActivity extends BaseActivity {
    private Context mContext;
    private EditText mEdtUserName, mEdtPassword, mEdtEmail, mEdtTelephone;
    private Button mBtnRegister, mBtnCancel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        mContext = RegisterActivity.this;
        initView();
    }

    /**
     * 初始化页面布局
     */
    private void initView() {
        mEdtEmail = (EditText) findViewById(R.id.edt_email);
        mEdtPassword = (EditText) findViewById(R.id.edt_password);
        mEdtTelephone = (EditText) findViewById(R.id.edt_telephone);
        mEdtUserName = (EditText) findViewById(R.id.edt_name);
        mBtnCancel = (Button) findViewById(R.id.btn_calcel);
        mBtnRegister = (Button) findViewById(R.id.btn_register);
        mBtnRegister = (Button) findViewById(R.id.btn_register);

        mBtnRegister.setOnClickListener(new clickListener());
        mBtnCancel.setOnClickListener(new clickListener());
    }

    /**
     * 页面控件点击事件
     */
    class clickListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_register: //注册
                    String userName = mEdtUserName.getText().toString().trim();
                    String password = mEdtPassword.getText().toString().trim();
                    String email = mEdtEmail.getText().toString().trim();
                    String telephone = mEdtTelephone.getText().toString().trim();
                    String sex = "男";
                    if (userName == null || userName.equals("")) { //用户名为空
                        Toast.makeText(mContext, mContext
                                .getString(R.string.null_user_name), Toast.LENGTH_SHORT).show();
                    } else if (password == null || password.equals("")) {  //密码为空
                        Toast.makeText(mContext, mContext
                                .getString(R.string.null_password), Toast.LENGTH_SHORT).show();
                    } else if (email == null || email.equals(""))  //邮箱地址为空
                    {
                        Toast.makeText(mContext, mContext
                                .getString(R.string.null_email), Toast.LENGTH_SHORT).show();
                    } else if (telephone == null || telephone.equals(""))  //手机号码为空
                    {
                        Toast.makeText(mContext, mContext
                                .getString(R.string.null_telephone), Toast.LENGTH_SHORT).show();
                    } else {
                        userRegister(userName, password, email, telephone); //注册用户
                    }
                    break;
                case R.id.btn_calcel:  //取消注册
                    finish();
                    break;

            }
        }
    }

    /**
     * 注册
     */
    public void userRegister(String userName, String password, String email, String telephone) {
        //显示进度框
        showProgressDialog(getString(R.string.loading));
        //调用注册接口
        SignUpCallback signUpCallback = new SignUpCallback() {
            public void done(AVException exception) {
                if (exception == null) { //没有异常，注册成功，跳转到首页
                    dismissProgressDialog();
                    showToastMessage(getString(R.string.register) + getString(R.string.success), mContext);
                    Intent mainIntent = new Intent(mContext, MainActivity.class);
                    startActivity(mainIntent);
                    finish();
                } else {
                    dismissProgressDialog();
                    switch (exception.getCode()) {
                        case 202: //用户名已存在
                            showDialogMessage(mContext.getString(R.string.user_exist), mContext);
                            break;
                        case 203://邮箱已注册
                            showDialogMessage(mContext.getString(R.string.email_exist), mContext);
                            break;
                        default: //网络连接错误
                            showDialogMessage(exception.getMessage(), mContext);
                            break;
                    }
                }
            }

        };
        //调用AVService注册接口
        AVService.signUp(userName, password, email, telephone, signUpCallback);
    }
}
